﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using eMeng;
using SQL;
public partial class Admin_UserEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin"] == null)
        {
            Response.Redirect("../Default.aspx");
        }
        else if (Convert.ToInt32(Session["GroupID"]) == 1)
        {
            List.Text = Session["List"].ToString();
            News_Edit.Text = Session["NewsType"].ToString();
            if (Session["OperationType"].ToString().Equals("New"))
            {
                rbAdmin.Checked = false;
                rbExaminator.Checked = false;
                rbEditor.Checked = true;
            }
            else if (Session["OperationType"].ToString().Equals("Edit"))
            {
                tbUserName.Enabled = false;

                int User_id = Convert.ToInt32(Session["News_id"]);
                NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                var users = from userinfo in dc.UserInfos
                            where userinfo.User_id.Equals(User_id)
                            select userinfo;

                foreach (var userinfo in users)
                {
                    tbUserName.Text = Convert.ToString(userinfo.UserName);
                    //JavaScript.JSDialog.Alert(this, userinfo.Password.Trim());

                    SymmCrypto sc = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);
                    string password = sc.Decrypting(userinfo.Password.Trim(), "binhai");

                    tbPassword.Text = password;
                    tbRePassword.Text = password;
                    if (1 == userinfo.Group_id)
                    {
                        rbAdmin.Checked = true;
                        rbExaminator.Checked = false;
                        rbEditor.Checked = false;
                    }
                    else if (2 == userinfo.Group_id)
                    {
                        rbAdmin.Checked = false;
                        rbExaminator.Checked = true;
                        rbEditor.Checked = false;
                    }
                    else if (0 == userinfo.Group_id)
                    {
                        rbAdmin.Checked = false;
                        rbExaminator.Checked = false;
                        rbEditor.Checked = true;
                    }
                }

                dc.Dispose();
               
            }
        }
        else
        {
            // Cannot log in this page

        }
    }
    protected void Submit_Button_Click(object sender, EventArgs e)
    {
        string username = Request.Form["ctl00$ContentPlaceHolder$tbUserName"];
        string password = Request.Form["ctl00$ContentPlaceHolder$tbPassword"];
        string rePassword = Request.Form["ctl00$ContentPlaceHolder$tbRePassword"];
        string postOrCheck = Request.Form["ctl00$ContentPlaceHolder$RadioGroup"];
        System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex("^[A-Za-z0-9]+$");

        if (!tbPassword.Text.Trim().Equals(tbRePassword.Text.Trim()))
        {
            JavaScript.JSDialog.Alert(this, "两次密码输入不一致！");
        }
        else if (tbPassword.Text.Trim().Length < 6 || tbPassword.Text.Trim().Length > 12)
        {
            JavaScript.JSDialog.Alert(this, "两次密码输入不一致！");
        }
        else if (!pattern.Match(tbPassword.Text.Trim()).Success)
        {
            JavaScript.JSDialog.Alert(this, "密码只能包含0-9数字，a-zA-Z字母");
        }
        else
        {

            if (Session["OperationType"].ToString().Equals("New"))
            {
                SymmCrypto sc = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);
                password = sc.Encrypting(password.Trim(), "binhai");
                UserInfo userInfo = new UserInfo();
                userInfo.UserName = username.Trim();
                userInfo.Password = password;
                if (postOrCheck == "rbAdmin")
                    userInfo.Group_id = 1;
                else if (postOrCheck == "rbExaminator")
                    userInfo.Group_id = 2;
                else if (postOrCheck == "rbEditor")
                    userInfo.Group_id = 0;
                SQLHelper.Insert(userInfo);
            }
            else if (Session["OperationType"].ToString().Equals("Edit"))
            {
                int User_id = Convert.ToInt32(Session["News_id"]);
                NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                var users = from userinfo in dc.UserInfos
                            where userinfo.User_id.Equals(User_id)
                            select userinfo;
                //JavaScript.JSDialog.Alert(this, "rbUserCheck " + rbUserCheck.Checked.ToString()+" rbUserPost "+rbUserPost.Checked.ToString());


                SymmCrypto sc = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);
                password = sc.Encrypting(password.Trim(), "binhai");

                foreach (var userinfo in users)
                {
                    userinfo.Password = password;

                    if (postOrCheck == "rbAdmin")
                        userinfo.Group_id = 1;
                    else if (postOrCheck == "rbExaminator")
                        userinfo.Group_id = 2;
                    else if (postOrCheck == "rbEditor") 
                        userinfo.Group_id = 0;
                }
                dc.SubmitChanges();
                dc.Dispose();
            }
            Response.Redirect("../Admin/AdminHome.aspx");

        }

    }
    protected void Return_Button_Click(object sender, EventArgs e)
    {
        this.RegisterClientScriptBlock("E", "<script language=javascript>history.go(-2);</script>");
    }

    protected void Logoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }

    protected void MenuLogoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }
    protected void PictureNewsManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "图片新闻编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void CollegeNewsManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "学院新闻编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void NoticeManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "信息公告编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void IndependentInstituteDynamicManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "独立学院动态编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void CampusDynamicManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "校园动态编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void ChangePassword_Click(object sender, EventArgs e)
    {
        Response.Redirect("ChangePassword.aspx");
    }
}
